<?php

declare(strict_types=1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;
use think\facade\Session;

class GmConslog extends Model
{
    use SoftDelete;
    protected $deleteTime = "delete_time";
    // 获取列表
    public static function getList()
    {
        $where = [];
        $limit = input('get.limit');

        // 如果不是超级管理员，添加限制条件
        if (Session::get('admin.id') != '1') {
            $where[] = ['c.id', '=', Session::get('admin.id')];
        }
        //按区服查找
        if ($qufu = input("qufu")) {
            $where[] = ["a.serverid", "=", $qufu];
        }
        //按账户查找
        if ($account = input("account")) {
            $where[] = ["a.account", "=", $account];
        }
        //按购买物品查找
        if ($itemid = input("itemid")) {
            $where[] = ["a.itemid", "like", "%" . $itemid . "%"];
        }
        if (Session::get('admin.id') != '1') {
            $list = self::table('gm_conslog')
                ->field([
                    'a.id' => 'id', 'a.serverid' => 'serverid', 'a.qufu' => 'qufu', 'a.account' => 'account', 'a.oldrmb' => 'oldrmb', 'a.nowrmb' => 'nowrmb', 'a.consrmb' => 'consrmb', 'a.itemid' => 'itemid', 'a.xftime' => 'xftime'
                ])
                ->alias('a')
                ->leftjoin('gm_tglogs b', 'a.account = b.baccount')
                ->leftjoin('admin_admin c', 'c.rlink = b.acode')
                ->order('a.id', 'desc')
                ->where($where)
                ->paginate($limit);
        } else {
            $list = self::table('gm_conslog')
                ->field([
                    'a.id' => 'id', 'a.serverid' => 'serverid', 'a.qufu' => 'qufu', 'a.account' => 'account', 'a.oldrmb' => 'oldrmb', 'a.nowrmb' => 'nowrmb', 'a.consrmb' => 'consrmb', 'a.itemid' => 'itemid', 'a.xftime' => 'xftime'
                ])
                ->alias('a')
                ->order('a.id', 'desc')
                ->where($where)
                ->paginate($limit);
        }


        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }
}
